/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *	 
 *	 Copyright (C) 2011
 *   @author christofferP
 */
package com.wirelesstransfer.server.ConnectedClients;



/**
 * Runnable that dissconnects client if it is inactive 
 * a specific time.
 * @author christofferP
 *
 */
public class CheckConnectedClient implements Runnable {
	
	private volatile ConnectedClient client;
	private volatile ConnectedClientsHandler connectedClientsHandler;
	private long sleepTime;

	/**
	 * Creates a runnable that dissconnects a client after a given time
	 * @param client client to be disconnected
	 * @param sleepTime time before dissconecting the client
	 */
	public CheckConnectedClient(ConnectedClient client, long sleepTime){
		this.client = client;
		this.sleepTime = sleepTime;
	}

	public void run() {
		
		System.err.println("Client " + client.getId() + " start run");
		try {
			Thread.sleep(sleepTime);
		} catch (InterruptedException e) {
			System.err.println("Client " + client.getId() + " still connected");
			Thread.currentThread().interrupt();
			return;
		}
		
		connectedClientsHandler = ConnectedClientsHandler.getInstance();
		if(connectedClientsHandler.getClientById(client.getId()) != null) {
			this.connectedClientsHandler.removeClient(client);
			System.err.println("Client " + client.getId() + " is disconnected");
		}
		
	}

}
